package cn.bugstack.trigger.job;

import cn.bugstack.domain.trade.service.ITradeSettlementOrderService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author liuxinzhi
 * @description:
 * @date 2025/8/11 15:04
 */
@Slf4j
@Service
public class GroupBuyNotifyJob {
    @Resource
    private ITradeSettlementOrderService tradeSettlementOrderService;

    @Resource
    private RedissonClient redissonClient;

    @Scheduled(cron = "0 0/10 * * * ?")
    private void exec() {
        RLock rLock = redissonClient.getLock("group_buy_market_notify_job_exec");
        try {
            if (rLock.tryLock(3, 0, TimeUnit.SECONDS)) {
                if(!rLock.isLocked()){
                    return;
                }
                Map<String, Integer> result = tradeSettlementOrderService.execSettlementNotifyJob();
                log.info("定时任务，回调通知拼团完结任务 result:{}", result);
            }
        } catch (Exception e) {
            log.error("定时任务，回调通知拼团完结任务失败 error:{}", e);
        } finally {
            if (rLock.isLocked() && rLock.isHeldByCurrentThread()) {
                rLock.unlock();
            }
        }
    }
}
